package org.op.wword;

import java.util.List;

import org.op.FrameView;
import org.op.nav.NavigationView;
import org.op.service.WwDocumentInfo;
import org.op.service.WwInfoObject;
import org.op.service.config.ConfigurationService;
import org.op.service.log.LoggerService;
import org.op.service.wwcache.WikiwordCacheService;
import org.op.util.MessageController;

public class AliasControllerImpl implements AliasController {

	private ConfigurationService configService;  // for retrieving configuration properties
	private MessageController messageController; // for retrieving messages and labels
	private WikiwordCacheService wwService;
	private FrameView frameView;
	private NavigationView navView;
	private AliasAddDialogView addDialog;
	private EditAliasDialogView editDialog;
	private LoggerService logger;
	
	public void addAlias() {
		String alias = null;

		WwInfoObject docInfo = null;
		docInfo = navView.getSelectedNodeObject();
		
		if (docInfo!=null && (docInfo instanceof WwDocumentInfo)) {
			// open a dialogue to capture a text string
			alias = addDialog.openAndGet();
			try {
				if (alias!=null) {
					wwService.addAlias(alias, docInfo.getFilepath());
				}
			} catch (Exception e) {
				frameView.showExceptionDialog(e.getLocalizedMessage());
			}
		} else {
			frameView.showInfoDialog(messageController.getMessage("no.wikiword.selected"));
		}
	}

	public void editAlias() {
		try {
			WwInfoObject docInfo = navView.getSelectedNodeObject();
			if (docInfo!=null && (docInfo instanceof WwDocumentInfo)) {
				// get aliases for docInfo				
				List<String> aliasList = wwService.getAliasesForDocument(docInfo.getFilepath());
				editDialog.setTitle(messageController.getLabel("alias.edit"));
				editDialog.setMessage(messageController.getMessage("alias.edit.message"));
				
				aliasList = editDialog.openAndGet(frameView.getFrame(), aliasList);
				wwService.setAliasesForFile(aliasList, docInfo.getFilepath());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ConfigurationService getConfigService() {
		return configService;
	}

	public void setConfigService(ConfigurationService configService) {
		this.configService = configService;
	}

	public MessageController getMessageController() {
		return messageController;
	}

	public void setMessageController(MessageController messageController) {
		this.messageController = messageController;
	}

	public FrameView getFrameView() {
		return frameView;
	}

	public void setFrameView(FrameView frameView) {
		this.frameView = frameView;
	}

	public WikiwordCacheService getWwService() {
		return wwService;
	}

	public void setWwService(WikiwordCacheService wwService) {
		this.wwService = wwService;
	}

	public AliasAddDialogView getAddDialog() {
		return addDialog;
	}

	public void setAddDialog(AliasAddDialogView addDialog) {
		this.addDialog = addDialog;
	}

	public NavigationView getNavView() {
		return navView;
	}

	public void setNavView(NavigationView navView) {
		this.navView = navView;
	}

	public EditAliasDialogView getEditDialog() {
		return editDialog;
	}

	public void setEditDialog(EditAliasDialogView editDialog) {
		this.editDialog = editDialog;
	}

	public LoggerService getLogger() {
		return logger;
	}

	public void setLogger(LoggerService logger) {
		this.logger = logger;
	}
	
}
